package com.jiazhong.shixi.caichanguanli.controller;

import com.jiazhong.shixi.caichanguanli.bean.Users;
import com.jiazhong.shixi.caichanguanli.mapper.UsersMapper;
import com.jiazhong.shixi.caichanguanli.util.JsonResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/user")
public class UsersController {
    @Resource
    private UsersMapper userMapper;

    @RequestMapping("/login")
    public JsonResult login(Users user) { //参数
        System.out.println(user);
        Users u = userMapper.login(user);
        if (u == null) {
            // 登陆失败
            JsonResult result
                    = new JsonResult(false, 427, null, "账号或者密码错误");
            return result;
        } else {
            // 登陆成功
            JsonResult result = new JsonResult(true, 200, u, "");
            return result;
        }
    }

    @RequestMapping("/add")
    public JsonResult add(Users user){
        userMapper.add(user);
        JsonResult result = new JsonResult(true,200,"success","");
        return result;
    }

    @RequestMapping("/list")
    public JsonResult list() {
        List<Users> list = userMapper.selectAll();
        return new JsonResult(true, 200, list, "");
    }

}

